<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Withdraw extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Withdraw;

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function status()
    {
        $id = input('ids');
        $status = input('status');
        $back = input('back');
        $res = $this->model->where(['id'=>$id])->find();
        if (!$res) $this->error('选项不存在！');
        if ($res['status'] == 2 && $status.'' !== '2') {
            $user = $this->CommonUserModel->where(['id'=>$res['uid']])->find();
            if ($user) {
                $newmoney = round($user['money']-$res['money'],2);
                $this->CommonUserModel->where(['id'=>$res['uid']])->update(['money'=>$newmoney]);
            }
            $back = '';
        }
        if ($res['status'] !== 2 && $status.'' == '2') {
            $user = $this->CommonUserModel->where(['id'=>$res['uid']])->find();
            if ($user) {
                $this->CommonUserModel->where(['id'=>$res['uid']])->update(['money'=>round($res['money']+$user['money'],2)]);
            }
        }
        $this->model->where(['id'=>$id])->update(['status'=>$status,'back'=>$back]);
        return json(['code'=>1,'msg'=>'操作成功','id'=>$id]);
    }
}
